import Variable from "../constants/index";
import { HttpMethod } from "../types/enum";
import { CodeResult } from "../types/user";

export default class RequestHttp {
  static get<T>(url: string, params: object) {
    return request<T>(url, HttpMethod.GET, params)
  }
  static post<T>(url: string, data: object) {
    return request<T>(url, HttpMethod.POST, data)
  }
  static put() {}
  static delete() {}
}
function request<T>(url: string, method: HttpMethod, data: object) {
  return new Promise<T>((resolve, reject) => {
    wx.request<CodeResult<T>>({
      url: Variable.BASE_URL + url,
      method,
      data,
      success(res) {
        if (res.data.message === 'OK') {
          resolve(res.data.data)
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'error'
          })
          reject(new Error(res.data.message))
        }
      },
      fail(res) {
        reject(new Error(res.errMsg))
      }
    })
  })
}